<?php
namespace  backend\components;

use  common\models\AQ;
use common\models\BaseModel;
use Yii;

class CRUDController extends Controller
{
   // public $layout = 'yiimain';
    public $error = null;
    public $user;
    /*
   *  CRUD actions
   */

    public function actionIndex()
    {
      return $this->dataGridView(AQ::ACTIVE);
    }
    public function actionRecyclebin()
    {
      return $this->dataGridView(AQ::DELETED);
    }
    protected function dataGridView($f){
      $this->layout = 'column_index';
      $searchModel = $this->findModel(null,true);
      $dataProvider = $searchModel->search(Yii::$app->request->queryParams,20,$f);
      return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
      ]);
    }

    public function actionView($id)
    {
        return $this->render('view', [
          'model' => $this->findModel($id),
        ]);
    }

    public function actionCreate()
    {
      return $this->CUaction();
    }

    public function actionUpdate($id)
    {
       return $this->CUaction($id);
    }

    protected function CUaction($id = 0){
      //$t_view = $id?'update':'create';
      $t_view = '_form';
      $model = $this->findModel($id);

      if ($model->load(Yii::$app->request->post()) && $model->save()) {
          $this->afterSaveAction($model);
        return $this->render('view',['model' => $model]);
      } else {
        return $this->render($t_view, [
          'model' => $model,
        ]);
      }
    }

//    public function beforeAJAX(){
//    }
    public function afterSaveAction($model){

    }

    public function actionDeleted($id)
    {
        $this->findModel($id)->deleted(AQ::DELETED);
        return $this->redirect($this->getReturnUrl());
    }
    public function actionCancel($id)
    {
        $this->findModel($id)->deleted(AQ::ACTIVE);
        return $this->redirect($this->getReturnUrl());
    }

  /**
   * @param null $id
   * @param bool $search
   * @return BaseModel
   */
  protected  function findModel($id = null,$search = false)
  {

  }

}